package com.dongdongshop.service.impl;

import com.dongdongshop.data.Page;
import com.dongdongshop.service.EsService;
import org.apache.dubbo.config.annotation.DubboService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@DubboService
@Service
public class EsServiceImpl implements EsService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public Page<Map<String,Object>> searchDoc(String title, Integer pageNum, Integer pageSize) {
        //创建搜索的请求
        SearchRequest request = new SearchRequest("dongdongshop");
        //创建一个用来添加查询条件的对象
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //匹配查询 match
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title",title);
        //把匹配查询的条件放入builder
        sourceBuilder.query(matchQueryBuilder);


        //5、高亮显示
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //field()中参数是需要加高亮的字段,需要和搜索的字段一一对应,不然没高亮
        highlightBuilder.field("title");
        //标签前缀
        highlightBuilder.preTags("<font color='red'>");
        //标签后缀
        highlightBuilder.postTags("</font>");
        //放入条件中
        sourceBuilder.highlighter(highlightBuilder);

        //放入请求
        request.source(sourceBuilder);
        //发送查询的请求
        SearchResponse response = null;
        try {
            response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //替换高亮
        SearchHit[] hits = response.getHits().getHits();
        List<Map<String,Object>> list = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();//这个是高亮的结果
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();//这个是我们正常的结果
            HighlightField field = highlightFields.get("title");
            Text[] fragments = field.getFragments();
            String title2 = "";
            if (fragments != null && fragments.length > 0){
                for (Text fragment : fragments) {
                    title2 += fragment;
                }
                //把高亮显示的结果替换到我们正常的结果中
                sourceAsMap.put("title",title2);
            }
            //返回前台的时候接收方便
            list.add(sourceAsMap);
        }

        Page<Map<String,Object>> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setRows(list);
        //高亮返回
        return page;
    }
}
